###Diplomatic Statements and the Strategic Use of Terrorism in Civil Wars###
###Levy, Dudley, Chen, Siegel###
###Journal of Conflict Resolution###
###R Script 4: Figure A4, Figure A5, and Figure A11 in the Online Appendix###

rm(list = ls())

#setwd("Replication")

library(dplyr)
library(lme4)
library(texreg)
library(ggplot2)
library(coefplot)
library(stargazer)
library(merTools)
library(ggeffects)
library(ggpubr)
library(gridGraphics)
library(patchwork)
library(egg)
library(ggpubr)
library(estimatr)
library(lubridate)
library(xtable)
library(Hmisc)
library(pastecs)
library(ggcorrplot)
source("Code/functions.R")
load("Data/polo_df.RData")
load("Data/ged_wide.RData")

####Figure A4####

#All attacks
l2.prop1 <- lm(ter_attacksum ~ pro_reb_res_lag2 + pro_gov_res_lag2 + 
                 prev_res_reb_lag3 + prev_res_gov_lag3 + 
                 any_reb_int_lag2 + any_gov_int_lag2 +
                 physint + lngdppc + lntpop + rebstrength2 + CivAttackProp_lag + 
                 coldwar,
                 data = polo_df)

l2.propXP1 <- lm(ter_attacksum ~ pro_reb_res_lag2 + pro_gov_res_lag2 + 
                   prev_res_reb_lag3 + prev_res_gov_lag3 + 
                   reb_X_d_lag2 + gov_X_d_lag2 +
                   physint + lngdppc + lntpop + rebstrength2 + CivAttackProp_lag + 
                   coldwar,
                   data = polo_df)

l2.propWP1 <- lm(ter_attacksum ~ pro_reb_res_lag2 + pro_gov_res_lag2 + 
                   prev_res_reb_lag3 + prev_res_gov_lag3 +
                   reb_W_d_lag2 + gov_W_d_lag2 +
                   physint + lngdppc + lntpop + rebstrength2 + CivAttackProp_lag + 
                   coldwar,
                   data = polo_df)

l2.propEP1 <- lm(ter_attacksum ~ pro_reb_res_lag2 + pro_gov_res_lag2 + 
                   prev_res_reb_lag3 + prev_res_gov_lag3 +
                   reb_E_d_lag2 + gov_E_d_lag2 +
                   physint + lngdppc + lntpop + rebstrength2 + CivAttackProp_lag + 
                   coldwar,
                   data = polo_df)

#Victims
l2.prop2 <- lm(ter_nkill ~ pro_reb_res_lag2 + pro_gov_res_lag2 + 
                 prev_res_reb_lag3 + prev_res_gov_lag3 + 
                 any_reb_int_lag2 + any_gov_int_lag2 +
                 physint + lngdppc + lntpop + rebstrength2 + CivAttackProp_lag + 
                 coldwar,
                 data = polo_df)

l2.propXP2 <- lm(ter_nkill ~ pro_reb_res_lag2 + pro_gov_res_lag2 + 
                   prev_res_reb_lag3 + prev_res_gov_lag3 + 
                   reb_X_d_lag2 + gov_X_d_lag2 +
                   physint + lngdppc + lntpop + rebstrength2 + CivAttackProp_lag + 
                   coldwar,
                   data = polo_df)

l2.propWP2 <- lm(ter_nkill  ~ pro_reb_res_lag2 + pro_gov_res_lag2 + 
                   prev_res_reb_lag3 + prev_res_gov_lag3 +
                   reb_W_d_lag2 + gov_W_d_lag2 +
                   physint + lngdppc + lntpop + rebstrength2 + CivAttackProp_lag + 
                   coldwar,
                   data = polo_df)

l2.propEP2 <- lm(ter_nkill ~ pro_reb_res_lag2 + pro_gov_res_lag2 + 
                   prev_res_reb_lag3 + prev_res_gov_lag3 +
                   reb_E_d_lag2 + gov_E_d_lag2 +
                   physint + lngdppc + lntpop + rebstrength2 + CivAttackProp_lag + 
                   coldwar,
                   data = polo_df)

#Soft Civilian Victims
l2.prop3 <- lm( attacksum_soft ~ pro_reb_res_lag2 + pro_gov_res_lag2 + 
                 prev_res_reb_lag3 + prev_res_gov_lag3 + 
                 any_reb_int_lag2 + any_gov_int_lag2 +
                  physint + lngdppc + lntpop + rebstrength2 + CivAttackProp_lag + 
                  coldwar,
               data = polo_df)

l2.propXP3 <- lm(attacksum_soft ~ pro_reb_res_lag2 + pro_gov_res_lag2 + 
                   prev_res_reb_lag3 + prev_res_gov_lag3 + 
                   reb_X_d_lag2 + gov_X_d_lag2 +
                   physint + lngdppc + lntpop + rebstrength2 + CivAttackProp_lag + 
                   coldwar,
                   data = polo_df)

l2.propWP3 <- lm(attacksum_soft ~ pro_reb_res_lag2 + pro_gov_res_lag2 + 
                   prev_res_reb_lag3 + prev_res_gov_lag3 +
                   reb_W_d_lag2 + gov_W_d_lag2 +
                   physint + lngdppc + lntpop + rebstrength2 + CivAttackProp_lag + 
                   coldwar,
                   data = polo_df)

l2.propEP3 <- lm(attacksum_soft ~ pro_reb_res_lag2 + pro_gov_res_lag2 + 
                   prev_res_reb_lag3 + prev_res_gov_lag3 +
                   reb_E_d_lag2 + gov_E_d_lag2 +
                   physint + lngdppc + lntpop + rebstrength2 + CivAttackProp_lag + 
                   coldwar,
                   data = polo_df)

#Figure
polo_1 <- LM_coef(modResults = list(l2.prop1, l2.propXP1, l2.propWP1, l2.propEP1), data = polo_df, 
                vars = c("prev_res_gov_lag3","prev_res_reb_lag3",
                         "pro_gov_res_lag2",
                         "pro_reb_res_lag2")) + 
  scale_x_discrete(labels=c("Previous Pro Gov Res,Count", 
                            "Previous Pro Reb Res,Count",
                            "Pro-Government Resolution",
                            "Pro-Rebel Resolution"
                            )) + 
  scale_shape_discrete(labels=c("Any",
                                "Troops", 
                                "Weapons",
                                "Economic")) +
  scale_color_discrete(labels=c("Any",
                                "Troops", 
                                "Weapons",
                                "Economic")) +
  ggtitle("All attacks")+theme(legend.position = "none") + labs(y = "")


polo_2 <- LM_coef(modResults = list(l2.prop2, l2.propXP2, l2.propWP2, l2.propEP2), data = polo_df, 
                  vars = c("prev_res_gov_lag3","prev_res_reb_lag3",
                           "pro_gov_res_lag2",
                           "pro_reb_res_lag2")) + 
  scale_x_discrete(labels=NULL) + 
  scale_shape_discrete(labels=c("Any",
                                "Troops", 
                                "Weapons",
                                "Economic")) +
  scale_color_discrete(labels=c("Any",
                                "Troops", 
                                "Weapons",
                                "Economic")) +
  ggtitle("Victims")


polo_3 <- LM_coef(modResults = list(l2.prop3, l2.propXP3, l2.propWP3, l2.propEP3), data =polo_df, 
                  vars = c("prev_res_gov_lag3","prev_res_reb_lag3",
                           "pro_gov_res_lag2",
                           "pro_reb_res_lag2"))+ 
 scale_x_discrete(labels=NULL)+ 
  scale_shape_discrete(labels=c("Any Material Support",
                                "Troops Intervention", 
                                "Weapons Intervention",
                                "Economic Intervention"))+
  scale_color_discrete(labels=c("Any Material Support",
                                "Troops Intervention", 
                                "Weapons Intervention",
                                "Economic Intervention"))+
  ggtitle("Soft Civilian Victims")+theme(legend.position = "none") + labs(y = "")

lags2_coplot <- ggarrange(polo_1, polo_2, polo_3,
                         ncol = 3, nrow = 1, byrow = FALSE)
ggsave("Figures/Fig_A4.jpeg", 
       plot = lags2_coplot, dpi = 600, width = 10, height = 5 )

####Figure A5####

polo_df$gwno_loc <- as.numeric(as.character(polo_df$gwno_loc))
polo_df <- left_join(polo_df, ged_wide, by = c("gwno_loc" = "country_id",
                                     "month" = "month"))

summary(polo_df$year)

#recode the NA from 1989-01
polo_df <- polo_df %>% 
   dplyr::mutate(state_violence = ifelse(is.na(state_violence), 0, state_violence),
                one_sided_violence = ifelse(is.na(one_sided_violence), 0, one_sided_violence),
                non_state_violence = ifelse(is.na(non_state_violence), 0, non_state_violence),
                percent_onesided = ifelse(is.na(percent_onesided), 0, percent_onesided),
                total_volience = ifelse(is.na(total_volience), 0, total_volience))

l2.prop <- lm(one_sided_violence ~ pro_reb_res_lag2 + pro_gov_res_lag2 + 
                prev_res_reb_lag3 + prev_res_gov_lag3 + 
                any_reb_int_lag2 + any_gov_int_lag2 +
                physint + lngdppc + lntpop + rebstrength2 + CivAttackProp_lag + 
                coldwar,
              data = polo_df)

l2.propXP <- lm(one_sided_violence ~ pro_reb_res_lag2 + pro_gov_res_lag2 + 
                  prev_res_reb_lag3 + prev_res_gov_lag3 + 
                  reb_X_d_lag2 + gov_X_d_lag2 +
                  physint + lngdppc + lntpop + rebstrength2 + CivAttackProp_lag + 
                  coldwar,
                data = polo_df)

l2.propWP <- lm(one_sided_violence ~ pro_reb_res_lag2 + pro_gov_res_lag2 + 
                  prev_res_reb_lag3 + prev_res_gov_lag3 +
                  reb_W_d_lag2 + gov_W_d_lag2 +
                  physint + lngdppc + lntpop + rebstrength2 + CivAttackProp_lag +
                  coldwar ,
                data = polo_df)

l2.propEP <- lm(one_sided_violence ~ pro_reb_res_lag2 + pro_gov_res_lag2 + 
                  prev_res_reb_lag3 + prev_res_gov_lag3 +
                  reb_E_d_lag2 + gov_E_d_lag2 +
                  physint + lngdppc + lntpop + rebstrength2 + CivAttackProp_lag +
                  coldwar ,
                data = polo_df)

ucdp <- LM_coef(modResults = list(l2.prop, l2.propXP, l2.propWP, l2.propEP), data =polo_df, 
                vars = c("prev_res_gov_lag3","prev_res_reb_lag3",
                         "pro_gov_res_lag2",
                         "pro_reb_res_lag2")) + 
                scale_x_discrete(labels=c("Previous Pro Gov Res,Count", 
                                          "Previous Pro Reb Res,Count",
                                          "Pro-Government Resolution",
                                          "Pro-Rebel Resolution")) +  
                scale_shape_discrete(labels=c("Any",
                                              "Troops", 
                                              "Weapons",
                                              "Economic")) +
                scale_color_discrete(labels=c("Any",
                                              "Troops", 
                                              "Weapons",
                                              "Economic")) +
                ggtitle("One-sided Violence")


ggsave("Figures/Fig_A5.jpeg", 
       plot = ucdp, dpi = 600, width = 9, height = 6)

####Figure A11####

corr_df <- polo_df %>% 
    dplyr::ungroup()%>%
    dplyr::select(ter_attacksum, ter_nkill , attacksum_soft, CivAttackProp, one_sided_violence)
corr_df <- na.omit(corr_df)

corr <- round(cor(corr_df), 2)
corr

p <- ggcorrplot(corr, method =  "circle",
           outline.color = "white",
           ggtheme = ggplot2::theme_bw) 
p + scale_x_discrete(labels = c("Polo and Gonźalez (2020): \n all attacks)",
                                "Polo and Gonźalez (2020): \n victims",
                                "Polo and Gonźalez (2020): \n soft civilian victims",
                                 "Proportion of civilian attacks", "UCDP: one-sided violence"))+
  scale_y_discrete(labels = c("Polo and Gonźalez (2020): \n all attacks)",
                              "Polo and Gonźalez (2020): \n victims",
                              "Polo and Gonźalez (2020): \n soft civilian victims",
                              "Proportion of civilian attacks", "UCDP: one-sided violence"))+
  ggtitle("Correlations among DVs")

ggsave("Figures/Fig_A11.jpeg", dpi = 600, width = 8, height = 8 )


